// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import EventManager from "../EventManager";
import GameConfig, { BlockData, COLOR } from "../GameConfig";
import Shape from "../shape/Shape";
import Utlis from "../Utlis";
import BlockGridRender from "./BlockGridRender";




const {ccclass, property} = cc._decorator;

@ccclass
export default class MapManager extends cc.Component {

    private static instance: MapManager;
    public static getInstance(): MapManager {
        if (!this.instance) {
            const node = new cc.Node("MapManager");
            cc.find("Canvas").addChild(node);
            this.instance = node.addComponent(MapManager);
        }
        return this.instance;
    }

    private bigBlockGrid:BlockData[][] = [];
    private smallBlockGrid:BlockData[][] = [];
    private isBigBlockNeedUpdate:boolean = false;
    private isSmallBlockNeedUpdate:boolean = false;

    private bigBlockGridParent:cc.Node = null;
    private smallBlockGridParent:cc.Node = null;

    private bigBlockGridRender:BlockGridRender = null;
    private smallBlockGridRender:BlockGridRender = null;

    public getBigBlockGridParent():cc.Node {
        return this.bigBlockGridParent;
    }

    public getSmallBlockGridParent():cc.Node {
        return this.smallBlockGridParent;
    }

    //private bigBlockGridRender:

    protected onLoad(): void {
        const designSize = cc.view.getDesignResolutionSize();
        

        //大格子
        const bigBlockCol = GameConfig.BIG_BLOCK_COL;
        const bigBlockRow = GameConfig.BIG_BLOCK_ROW;//Math.floor(designSize.height / GameConfig.BIG_BLOCK_SIZE);
        for (let j = 0; j < bigBlockRow; j++) {
            if (!this.bigBlockGrid[j]) {
                this.bigBlockGrid[j] = [];
            }
            for(let i = 0; i < bigBlockCol; i++) {
                this.bigBlockGrid[j][i] = {
                    x: i,
                    y: j,
                    color: COLOR.Null,
                }
            }
        }

        //小格子
        const smallBlockCol = GameConfig.SMALL_BLOCK_SIZE;
        const smallBlockRow = Math.floor(designSize.height / GameConfig.SMALL_BLOCK_SIZE);
        for (let j = 0; j < smallBlockRow; j++) {
            if (!this.smallBlockGrid[j]) {
                this.smallBlockGrid[j] = [];
            }
            for(let i = 0; i < smallBlockCol; i++) {
                this.smallBlockGrid[j][i] = {
                    x: i,
                    y: j,
                    color: COLOR.Null,
                }
            }
        }

        

        let widget = this.node.addComponent(cc.Widget);
        Utlis.widgetAdapterFullScene(widget);

        this.bigBlockGridParent = new cc.Node("BigBlockGridParent");
        widget = this.bigBlockGridParent.addComponent(cc.Widget);
        Utlis.widgetAdapterFullScene(widget);

        this.smallBlockGridParent = new cc.Node("SmallBlockGridParent");
        widget = this.smallBlockGridParent.addComponent(cc.Widget);
        Utlis.widgetAdapterFullScene(widget);

        this.node.addChild(this.bigBlockGridParent);
        this.node.addChild(this.smallBlockGridParent);

        this.bigBlockGridRender = new BlockGridRender(this.bigBlockGridParent);
        this.smallBlockGridRender = new BlockGridRender(this.smallBlockGridParent);

        EventManager.getInstance().on(GameConfig.EVENT_NAME.SHAPE_FALL_TO_BOTTOM, this.onShapeFallToBottom, this);

        console.warn("网格初始化完成");
    }

    public setBigBlockData(block:BlockData) {
        if (this.bigBlockGrid[block.y] == null) {
            console.error(`bigBlockGrid[${block.y}] is null`);
            return;
        }
        if (this.bigBlockGrid[block.y][block.x] == null) {
            console.error(`bigBlockGrid[${block.y}][${block.x}] is null`);
            return;
        }
        this.bigBlockGrid[block.y][block.x] = block;
        this.isBigBlockNeedUpdate = true;
    }

    protected update(dt: number): void {
        if (this.isBigBlockNeedUpdate) {
            this.updateBigBlockGrid();
            this.isBigBlockNeedUpdate = false;
        }

        if (this.isSmallBlockNeedUpdate) {
            this.updateSmallBlockGrid();
            this.isSmallBlockNeedUpdate = false;
        }
    }

    private updateBigBlockGrid() {
        this.bigBlockGridRender.updateGridRender(this.bigBlockGrid);
    }

    private updateSmallBlockGrid() {
        this.smallBlockGridRender.updateGridRender(this.smallBlockGrid);
    }

    public hasBlockAt(gridPos:cc.Vec2) {
        if (this.bigBlockGrid[gridPos.y] == null) {
            return false;
        }
        if (this.bigBlockGrid[gridPos.y][gridPos.x] == null) {
            return false;
        }

        if (this.bigBlockGrid[gridPos.y][gridPos.x].color == COLOR.Null) {
            return false;
        }

        return true;
    }

    private onShapeFallToBottom(shape:Shape) {
        // console.error("方块落地了 分解成小方块");
    }
}
